Grasshopper-C# 贪吃蛇

小zz Arch Z 2020-02-19原文 发表于




据说学编程的同学都必做贪吃蛇?入门级别的经典游戏拿来练练手也无妨。


Github上已经有很多贪吃蛇等各类游戏,用各种语法和思路写成的的代码范例。想学习,练习或者交流的话Github是个非常好的平台。


https://github.com


不过我这次写的贪吃蛇是基于我自己的理解写的,没有参照例子,也比较简单。里面可能有可以优化或者不足的地方,欢迎指出。




代码如下:


输入(方向和重置):

pManager.AddIntegerParameter("zDirection", "zDirection", "Input a zDirection component.", GH_ParamAccess.item);

pManager.AddBooleanParameter("zReset", "zReset", "Connect a button to reset.", GH_ParamAccess.item);


输出(蛇身和食物):

pManager.AddCurveParameter("Snake", "Snake", "The snake body curve.", GH_ParamAccess.list);

pManager.AddCircleParameter("Food", "Food", "The food as circle.", GH_ParamAccess.item);


Persistent Data:

Point3d headPoint = new Point3d(0, 0, 0);

List<Point3d> bodyPoints = new List<Point3d>() { new Point3d(0, 0, 0) };

int seed = 0;

List<Point3d> zBodyPoints = new List<Point3d>();



主体:

{

            //Input

            int zDirection = 0;

            bool zReset = true;

            bool judge = DA.GetData("zDirection", ref zDirection);

            bool judge2 = DA.GetData("zReset", ref zReset);

            if (!judge) AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Please add a MDsilder to control the direction. Must be setted as -1 to 1.");

            if (!judge2) AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Please connect a button to the reset!");

            AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Please remenber add a timer to it. Suggested time interval:1s.");



            List<Curve> bodies = new List<Curve>();

            Random foodRandomNumber = new Random(seed);


            //Reset

            if (zReset)

            {

                headPoint = new Point3d(0, 0, 0);

                List<Point3d> bodyPoints = new List<Point3d>();

            }


            //Direction

            else

            {

                Vector3d direction = new Vector3d(0, 0, 0);

                if (zDirection == 1)

                { direction[1] = 1; }

                if (zDirection == 2)

                { direction[1] = -1; }

                if (zDirection == 3)

                { direction[0] = -1; }

                if (zDirection == 4)

                { direction[0] = 1; }

                bodyPoints.Add(headPoint);

                headPoint += direction * 10;


                //Food section below

                double foodRandomX = foodRandomNumber.Next(-100, 100);

                double foodRandomY = foodRandomNumber.Next(-100, 100);

                Point3d foodPoint = new Point3d(foodRandomX, foodRandomY, 0);

                Circle FoodCircle = new Circle(foodPoint, 10);


                //Eating judge

                if (foodPoint.DistanceTo(headPoint) < 12) { seed++; }

                else { bodyPoints.RemoveRange(0, 1); }


                foreach (Point3d point in bodyPoints)

                {

                    Circle body = new Circle(point, 5);

                    bodies.Add(body.ToNurbsCurve());

                }

                Curve[] zSnake = Curve.CreateBooleanUnion(bodies, 1); 

                List<Curve> output = new List<Curve>(zSnake);


                //Output

                DA.SetData("Food", FoodCircle);

                DA.SetDataList("Snake", output);


            }




希望能给你带来点启发。




--- Growing, Growing, Brighter Everyday ! ---